//
//  ZDTopView.swift
//  1026SwiftSinaWeiBo
//
//  Created by Apple on 15/10/31.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
import SDWebImage

class ZDTopView: UIView {
    
    //设置全局margin
    let margin: CGFloat = 8
    
    var status: ZDStatus? {
    
        didSet {
        
            let user = status?.user
            
            if let imageURL = user?.profile_image_url {
            
                iconView.zd_setImageWithURL(NSURL(string: imageURL))
            }
            
            nameLabel.text = user?.name
            
            timeLabel.text = NSDate.dateWithSinaString(status?.created_at ?? "").sinaDateDescription()
            
            sourseLabel.text = status?.source
//            sourseLabel.text = "传智微博"
            
            vipView.image = user?.vipImage

            verifiedView.image = user?.verifieImage
        }
    }
    
   
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 添加子控件
    private func prepareUI() {
        
//        backgroundColor = UIColor.brownColor()
        
        //添加子控件
        addSubview(separatView)
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourseLabel)
        addSubview(vipView)
        addSubview(verifiedView)
        
        //布局约束
        //separatView
        separatView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: self, size: CGSize(width: UIScreen.width, height: 10))
        
        //iconView
        iconView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: separatView, size: CGSize(width: 35, height: 35), offset: CGPoint(x: margin, y: margin))
        
        //nameLabel
        nameLabel.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: iconView, size: nil, offset: CGPoint(x: margin, y: 0))
        
        //timeLabel
        timeLabel.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: iconView, size: nil, offset: CGPoint(x: margin, y: 0))
        
        //resourseLabel
        sourseLabel.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: timeLabel, size: nil, offset: CGPoint(x: margin, y: 0))
        
        //vipView
        vipView.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: nameLabel, size: CGSize(width: 14, height: 14), offset: CGPoint(x: margin, y: 0))
        
        //verifiedView
        verifiedView.ff_AlignInner(type: ff_AlignType.BottomRight, referView: iconView, size: CGSize(width: 17, height: 17), offset: CGPoint(x: 8.5, y: 8.5))
    }
    
    
    //MARK: 懒加载
    private lazy var iconView: UIImageView = UIImageView()
    
    
    private lazy var nameLabel: UILabel = UILabel(fontSize: 14, color: UIColor.darkGrayColor())
    
    
    private lazy var timeLabel: UILabel = UILabel(fontSize: 9, color: UIColor.orangeColor())
    
    
    private lazy var sourseLabel: UILabel = UILabel(fontSize: 9, color: UIColor.lightGrayColor())
    
    
    private lazy var vipView: UIImageView = UIImageView()
    
    
    private lazy var verifiedView: UIImageView = UIImageView()
    
    
    private lazy var separatView: UIView = {
    
        let view = UIView()
        
        view.backgroundColor = UIColor(white: 0.9, alpha: 1.0)
        
        return view
    }()

}
